import com.fasterxml.jackson.databind.ObjectMapper;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 22479
 * Date: 2022-12-01
 * Time: 12:30
 */
class JudgeDelete {
    public String flg;
}

@WebServlet("/delete")
public class BlogDeleteServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //这个方法用于删除一篇博客
        JudgeDelete judge = new JudgeDelete();
        //1.获取当前会话
        HttpSession session = req.getSession(false);
        if(session == null) {
            //无会话,当前是未登录状态
            resp.setStatus(403);
            resp.sendRedirect("login.html");
            return;
        }
        //这里的user对象需要判断是否存在
        User user = (User) session.getAttribute("user");
        if(user == null) {
            resp.setStatus(403);
            resp.sendRedirect("login.html");
            return;
        }
        //获取到blogId
        String blogId = req.getParameter("blogId");
        //查询出这个blogId对应的Blog对象
        BlogDao blogDao = new BlogDao();
        Blog blog = blogDao.selectOne(Integer.parseInt(blogId));

        /*
        优化版本 - 是当前作者才添加删除按钮 不是当前作者没有删除按钮 所以不判断了
         */
        //4.判定登陆用户是否就是文章作者 -- 是
        if(user.getUserId() != blog.getUserId()) {
            return;
        }
        //5.执行删除操作
        blogDao.delete(Integer.parseInt(blogId));
        //6.返回302重定向
        resp.sendRedirect("blog_list.html");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        JudgeDelete judge = new JudgeDelete();
        ObjectMapper objectMapper = new ObjectMapper();
        //1.获取当前会话
        HttpSession session = req.getSession(false);
        if(session == null) {
            //无会话,当前是未登录状态
            resp.setStatus(403);
            resp.sendRedirect("login.html");
            return;
        }
        //这里的user对象需要判断是否存在
        User user = (User) session.getAttribute("user");
        if(user == null) {
            resp.setStatus(403);
            resp.sendRedirect("login.html");
            return;
        }
        //获取到blogId
        String blogId = req.getParameter("blogId");
        //查询出这个blogId对应的Blog对象
        BlogDao blogDao = new BlogDao();
        Blog blog = blogDao.selectOne(Integer.parseInt(blogId));

        /*
        优化版本 - 是当前作者才添加删除按钮 不是当前作者没有删除按钮 所以不判断了
         */
        //4.判定登陆用户是否就是文章作者 -- 是
        if(user.getUserId() != blog.getUserId()) {
            judge.flg = "false";
            resp.setContentType("application/json;charset=utf8");
            resp.getWriter().write(objectMapper.writeValueAsString(judge));
        } else {
            judge.flg = "true";
            resp.setContentType("application/json;charset=utf8");
            resp.getWriter().write(objectMapper.writeValueAsString(judge));
        }
    }
}
